/**
 * 测试用例获取接口测试用例
 *
 * Created by snowingsea on 2020/03/13.
 */
const chai = require('chai');
const should = chai.should(); // eslint-disable-line no-unused-vars
chai.use(require('chai-http'));

const common = require('./../../common');
const Administrator = require('./../../../models/administrator');
const uri = require('./../uri');
const benchmarks = [
  require('./../../../benchmark/generate-tests/self_designed'),
  /*
   * uint8_t [4][4], uint8_t[4][4]
   * @type in generated testcase have blank,but in typesystem there is no blank
   * so the type are not matched
   * temporarily comment this testcase
   */
  // require('./../../../benchmark/generate-tests/logic_bombs'),
];

const tests = require('./../../../../phoenix/models/tests');

module.exports = describe('/GET tests/:id.json', function () {
  const administrator = new Administrator();
  let project = {};
  let testId;
  const newUserIds = [];
  function url(testId) {
    return `/tests/${testId}.json`;
  }

  before(function (done) {
    administrator.login()
      .then(() => administrator.createRandomTester())
      .then((tester) => {
        newUserIds.push(tester.userId);
        return tester.createRandomProject();
      })
      .then((res) => {
        project = res;
        done();
      })
      .catch(done);
  });
  after(function (done) {
    administrator.removeUsers(newUserIds)
      .then(() => {
        done();
      })
      .catch(done);
  });

  common.should.be.user(uri, url, 'get');
  common.should.be.tester(uri, url, 'get');

  // 正常发起请求
  benchmarks.forEach((benchmark) => {
    [
      tests.testKind.statement,
      tests.testKind.branch,
      tests.testKind.mcdc,
      tests.testKind.statement + tests.testKind.branch + tests.testKind.mcdc,
    ].forEach((coverages) => {
      it(`it should get tests of function in ${benchmark.name} by coverages ${coverages}`, function (done) {
        let projectVersion;
        let allTests;
        project.createRandomVersion()
          .then((res) => {
            projectVersion = res;
            const { config } = benchmark;
            config.coverages = coverages;
            return projectVersion.getAllTests(benchmark);
          })
          .then((res) => {
            allTests = res;
            testId = res[0].testId;
            return project.creator.login();
          })
          .then(() => {
            const promises = allTests.map((test) => {
              const { testId } = test;
              return new Promise((resolve, reject) => {
                let funcVariables;
                let fileTypes;
                const { accessToken } = project.creator.token;
                test.function.getVariables(accessToken)
                  .then((res) => {
                    funcVariables = res;
                    const { fileId } = test.function;
                    return test.function.rootDirectory.getFileTypes(fileId, accessToken);
                  })
                  .then((res) => {
                    fileTypes = res;
                    chai.request(uri)
                      .get(url(testId))
                      .set('Authorization', `Bearer ${accessToken}`)
                      .end((err, res) => {
                        res.should.have.status(200);
                        common.should.be.testCaseMessage(res.body);
                        const { data } = res.body;
                        common.should.complete.functionVariables(data, funcVariables, fileTypes);
                        resolve();
                      });
                  })
                  .catch(reject);
              });
            });
            return Promise.all(promises);
          })
          .then(() => {
            done();
          })
          .catch(done);
      });
    });
  });

  // 不可获取他人项目的测试用例
  it('it should fail to get test when the project is not yours', function (done) {
    let newUser;
    administrator.createRandomTester()
      .then((tester) => {
        newUser = tester;
        newUserIds.push(tester.userId);
        return tester.getToken();
      })
      .then(() => {
        chai.request(uri)
          .get(url(testId))
          .set('Authorization', `Bearer ${newUser.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(404);
            common.should.be.errorMessage(res.body);
            done();
          });
      })
      .catch(done);
  });

  // 不可获取不存在的项目文件的测试用例
  it('it should fail to get test when the project is not exist', function (done) {
    new Promise((resolve) => {
      chai.request(uri)
        .get(url('000000000000000000000000'))
        .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
        .end((err, res) => {
          res.should.have.status(404);
          common.should.be.errorMessage(res.body);
          resolve();
        });
    })
      .then(done)
      .catch(done);
  });
});
